package com.ithuameng.admin.app.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ithuameng.admin.app.system.dao.UserRoleMapper;
import com.ithuameng.admin.app.system.model.UserRole;
import com.ithuameng.admin.app.system.service.IUserRoleService;
import com.ithuameng.admin.constants.CommonConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * User role service implementation
 *
 * @author ithuameng
 */
@Transactional
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public List<Long> listRoleByUserId(Long userId) {
        return super.baseMapper.listRoleByUserId(userId);
    }

    @Override
    public List<Long> listUserByRoleId(Long roleId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserRole.ROLE_ID, roleId);
        List<UserRole> list = super.list(queryWrapper);
        return list.stream().map(UserRole::getUserId).collect(Collectors.toList());
    }

    @Override
    public void removeByUserId(Long userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserRole.USER_ID, userId);
        super.remove(queryWrapper);
        String key = CommonConstants.PERMESSION_PREFIX + Long.toString(userId);
        if (stringRedisTemplate.hasKey(key)) {
            stringRedisTemplate.delete(key);
        }
    }

    @Override
    public void removeRoleByUserId(Long roleId, Long userId) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(UserRole.USER_ID, userId);
        queryWrapper.eq(UserRole.ROLE_ID, roleId);
        super.remove(queryWrapper);
        String key = CommonConstants.PERMESSION_PREFIX + Long.toString(userId);
        if (stringRedisTemplate.hasKey(key)) {
            stringRedisTemplate.delete(key);
        }
    }

    @Override
    public void removeRoleByUserId(Long roleId, Long[] userIds) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in(UserRole.USER_ID, Arrays.asList(userIds));
        queryWrapper.eq(UserRole.ROLE_ID, roleId);
        super.remove(queryWrapper);
        for (int i = 0; i < userIds.length; i++) {
            String key = CommonConstants.PERMESSION_PREFIX + Long.toString(userIds[i]);
            if (stringRedisTemplate.hasKey(key)) {
                stringRedisTemplate.delete(key);
            }
        }
    }

    @Override
    public void assignRole2User(Long roleId, Long[] userIds) {
        if (userIds == null || userIds.length == 0) {
            return;
        }
        List<UserRole> list = new ArrayList<>();
        for (Long userId : userIds) {
            UserRole ur = new UserRole(userId, roleId);
            list.add(ur);
        }
        super.saveBatch(list);
        for (int i = 0; i < userIds.length; i++) {
            String key = CommonConstants.PERMESSION_PREFIX + Long.toString(userIds[i]);
            if (stringRedisTemplate.hasKey(key)) {
                stringRedisTemplate.delete(key);
            }
        }
    }

}
